define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'official/user/index',
                    add_url: 'official/user/add',
                    edit_url: 'official/user/edit',
                    del_url: 'official/user/del',
                    table: 'official',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id'), sortable: true},
                    /*    {field: 'group.name', title: __('Group')},*/
                        // {field: 'username', title: __('名称'), operate: 'LIKE'},
                        {field: 'openid', title: __('openid'), operate: 'LIKE'},
                     /*   {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},*/
                    /*    {field: 'email', title: __('Email'), operate: 'LIKE'},*/
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
						{field: 'is_member', title: __('是否是会员'), searchList:{"1":__('是'),"0":__('否')},formatter: Table.api.formatter.normal, operate: false},
						{field: 'product', title: __('订购产品'), operate: false},
                      /*  {field: 'website_url', title: __('Website_url'), formatter: Table.api.formatter.url},*/
                        // {field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'last_ip', title: __('登录ip'), formatter: Table.api.formatter.search, operate: false},
                        {field: 'login_num', title: __('登录次数'), formatter: Table.api.formatter.search, operate: false},
                        {field: 'last_login', title: __('最后登录时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, operate: false},
                    ]
                ],
				//禁用默认搜索
				search: false,
				//启用普通表单搜索
				commonSearch: true,
				//可以控制是否默认显示搜索单表,false则隐藏,默认为false
				searchFormVisible: true,
				showSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

$(document).on("click", "#btn", function(){
    var openid = $("#c-openid") .val();
    var id = $("#id").val();
    if (!id) {
        Toastr.error('id 不能为空');return false;
    }
    if (!openid) {
        Toastr.error('openid 不能为空');return false;
    }
    $.post({
        url: 'users/openid',
        type: 'POST',
        dataType: 'json',
        data: {id: id},
        cache: false,
        success: function (ret) {
            if(ret.code == 1){
                Toastr.success('设置成功');
                window.setTimeout(function () {
                    window.location.reload();
                }, 3000);
            }else{
                Toastr.error('设置失败');
            }
        }, error: function () {
            Toastr.error(__('Network error'));
        }
    });
});
